import pymysql
from pymysql.cursors import DictCursor
from config import HOST, USER, PASSWORD, DATABASE
 
# 打开数据库连接
def connect():
    conn = pymysql.connect(host=HOST,
                         user=USER,
                         password=PASSWORD,
                         database=DATABASE,
                         cursorclass=DictCursor  # 指定游标类型为字典游标
                         )
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = conn.cursor()
    return conn, cursor

conn, cursor = connect()
 